package com.mk.thread;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.nutz.dao.Dao;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.mk.MyNutTestRunner;
import com.mk.car.entity.Car;
import com.mk.car.service.CarService;

/**
 * 线程池
 * @author 上官雅晴
 *
 */ 
@RunWith(MyNutTestRunner.class)
@IocBean
public class ThreadPoolDemo {
	@Inject
    Ioc ioc;
    @Inject
    Dao dao;
    @Inject
    CarService carDao;
    
	// 可缓存线程池
	ExecutorService executorService = Executors.newFixedThreadPool(2);
	
	@Before
	public void beforeDone() {
		
	}
	
	@Test
	public void test1() throws InterruptedException {
		Long start = System.currentTimeMillis();
		executorService.submit(()->{
			List<Car> cars = carDao.listCar(1, 800);
		});
		executorService.submit(()->{
			List<Car> cars1 = carDao.listCar(2, 800);
		});
		
		executorService.shutdown();
		executorService.awaitTermination(1200, TimeUnit.MINUTES);
		
		Long end = System.currentTimeMillis();
		System.out.println("时间间隔111: "+(end-start));
	}
	
	@Test
	public void test2() {
		Long start = System.currentTimeMillis();
		List<Car> cars = carDao.listCar(1, 800);
		List<Car> cars1 = carDao.listCar(2, 800);
		
		
		Long end = System.currentTimeMillis();
		System.out.println("时间间隔: "+(end-start));
	}
	
	
	
	
	
}
